package org.qpcr.community.project.web;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.qpcr.community.project.model.entity.DataAnalysisDashboardChartsEntity;
import org.qpcr.community.project.request.DataAnalysisDashboardChartsSaveRequest;
import org.qpcr.community.project.request.DataAnalysisDashboardQuery;
import org.qpcr.community.project.response.DataAnalysisDashboardChartsDetail;
import org.qpcr.community.project.response.DataAnalysisDashboardChartsResponse;
import org.qpcr.community.project.service.DataAnalysisDashboardChartsService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.List;

@RestController
@RequestMapping({"/project/data-analysis-dashboard-charts", "/project/data-analysis/dashboard-charts"})
@Authorize(ignore = true)
@Resource(id = "data-analysis-dashboard-charts", name = "数据分析图表")
@Slf4j
@Tag(name = "数据分析图表接口")
@RequiredArgsConstructor
public class DataAnalysisDashboardChartsController  implements ReactiveServiceCrudController<DataAnalysisDashboardChartsEntity, String> {

    @Getter
    private final DataAnalysisDashboardChartsService service;

    @PostMapping("save-all")
    @QueryAction
    @Authorize(ignore = true)
    public Mono<Void> saveAll(@RequestBody DataAnalysisDashboardChartsSaveRequest request) {
        return service.saveAll(request);
    }

    @PostMapping("list-by-group-id")
    @QueryAction
    @Authorize(ignore = true)
    public Mono<List<DataAnalysisDashboardChartsResponse>> query(@RequestBody DataAnalysisDashboardQuery query) {
        return Mono.fromCallable(() -> service.listByGroupIdSync(query))
                   .subscribeOn(Schedulers.boundedElastic());
    }

    @PostMapping("delete-by-id")
    @QueryAction
    @Authorize(ignore = true)
    public Mono<Integer> deleteById(@RequestParam String id) {
        return service.deleteAllById(id);
    }

    @PostMapping("rename")
    @QueryAction
    @Authorize(ignore = true)
    public Mono<Integer> rename(@RequestBody DataAnalysisDashboardChartsSaveRequest request) {
        return service.rename(request);
    }


    @PostMapping("clone")
    @QueryAction
    @Authorize(ignore = true)
    public Mono<Integer> clone(@RequestParam String id) {
        return service.cloneChart(id);
    }

    @GetMapping("detail")
    @QueryAction
    @Authorize(ignore = true)
    public Mono<DataAnalysisDashboardChartsDetail> detail(@RequestParam String id) {
        return service.detail(id);
    }
}
